import com.sun.deploy.uitoolkit.impl.fx.AppletStageManager;

import java.util.Arrays;

public class Text {


    public static void main(String[] args) {
         int a = 10;
         int b = 20;

         int ret = add(a,b);
        System.out.println("ret = "+ ret);
    }

    public static int add(int a,int b) {
        return a+b;
    }
    public static void main(String[] args) {
        int sum = 0;
        for(int i = 1; i<=5;i++){
            sum += fac(i);
        }
        System.out.println("sum = "+sum);
    }

    public static int fac(int n) {
        System.out.println("计算"+n+"的阶乘");
        int result = 1;
        for(int i = 1;i<=n;i++){
            result*=i ;
        }
        return result;

    }
    public static void main(String[] args) {
        int[] arr = {10,20};
        swap(arr);
        System.out.println("a = "+arr[0]+" b = "+arr[1]);
    }

    public static void swap(int[] arr) {
        int tmp = arr[0];
        arr[0] = arr[1];
        arr[1] = tmp;

    }

    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int ret = add(a,b);
        System.out.println("ret = "+ ret);
        double a2 = 10.5;
        double b2 = 20.5;
        double ret2 =  add(a2,b2);
        System.out.println("ret2 = " + ret2);

    }

    public static int add(int x ,int y ) {
        return x+y;
    }

    public static double add(double x,double y) {
        return  x+y;
    }


    public static void main(String[] args) {
        int n = 5;
        int ret = factor(n);
        System.out.println("ret = "+ ret);

    }

    public static int factor(int n) {
        if(n==1){
            return 1;
        }
        return n*factor(n-1);
    }


    public static void main(String[] args) {
        int num = 1234;
        print(num);

    }

    public static void print(int num) {
        if(num>10){
            print(num/10);
        }
        System.out.println(num%10);
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3};
        for(int x :arr){
            System.out.println(x);
        }
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3};
        func(arr);
        System.out.println("arr[0] = "+arr[0]);
    }

    public static void func(int[] a) {
            a[0]  = 100;
        System.out.println("a[0] = "+a[0]);
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3};
        transform(arr);
        printArray(arr);
    }

    public static void printArray(int[] arr) {
        for(int i = 0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }
    public static void transform(int[] arr){
        for(int i = 0;i<arr.length;i++){
            arr[i] *= arr[i]*2;
        }
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3};
        int[] output = transform(arr);
        printArray(output);
    }

    public static void printArray(int[] arr) {
        for(int i = 0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }

    public static int[] transform(int[] arr) {
        int[] ret = new int[arr.length];
        for(int i = 0;i<ret.length;i++){
            ret[i] = arr[i]*2;
        }
        return ret;
    }


    //数组转字符串Arrays.toString
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        String newArray = Arrays.toString(arr);
        System.out.println(newArray);
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(toString(arr));
    }

    public static String toString(int[] arr) {
        String ret = "[";
        for (int i = 0; i < arr.length; i++) {
            ret += arr[i];
            if(i!=arr.length-1){
                ret+=", ";
            }
        }
        ret += "]";
        return ret;
    }


    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        int[] newArray = Arrays.copyOf(arr, arr.length);
        System.out.println("newArr:"+Arrays.toString(newArray));
        arr[0] = 10;
        System.out.println("arr:"+Arrays.toString(arr));
        System.out.println("newArr:"+Arrays.toString(newArray));
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(max(arr));

    }

    public static int max(int[] arr) {
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }

        }
        return max;
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(avg(arr));
    }

    public static double avg(int[] arr) {
        int sum = 0;
        for(int x:arr){
            sum+=x;
        }
        return sum*1.0/arr.length;
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3,10,5,6};
        System.out.println(find(arr,10));
    }

    public static int find(int[] arr ,int tofind) {
        for(int i = 0;i<arr.length;i++){
            if(tofind==arr[i]){
                return i;
            }

        }
        return -1;


    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(binarySearch(arr,6));
    }

    public static int binarySearch(int[] arr,int toFind) {
        int left =  0;
        int right = arr.length-1;
        while(left<=right){
            int mid = (left+right)/2;
            if(arr[mid]>toFind){
                right = mid -1;
            } else if (arr[mid]<toFind) {
                left =  mid+1;
            }else if(arr[mid] == toFind){
                return mid;
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3,4};
        reverse(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static int[] reverse(int[] arr) {

            int right = arr.length-1;
            int left = 0;
            while(left<right){
                int tmp = arr[left];
                arr[left] = arr[right];
                arr[right] = tmp;
                left++;
                right--;
            }

        return arr;
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        transform(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void transform(int[] arr) {
        int left =  0;
        int right = arr.length -1 ;
        while(left<right){
            while(left<right&&arr[left]%2==0){
                left++;
            }
            while(left<right&&arr[right]%2!=0){
                right--;
            }
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }

    }
}



















